package com.electromagnetic.industry.software.manage.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.electromagnetic.industry.software.common.annotations.UserOperation;
import com.electromagnetic.industry.software.common.enums.DataOwnEnum;
import com.electromagnetic.industry.software.common.enums.UserOperationModuleEnum;
import com.electromagnetic.industry.software.common.resp.ElectromagneticResult;
import com.electromagnetic.industry.software.common.util.ElectromagneticResultUtil;
import com.electromagnetic.industry.software.common.util.UserThreadLocal;
import com.electromagnetic.industry.software.manage.pojo.models.EdFileRelation;
import com.electromagnetic.industry.software.manage.pojo.req.CheckNameUniqueRequest;
import com.electromagnetic.industry.software.manage.pojo.req.UpdateRelationDTO;
import com.electromagnetic.industry.software.manage.service.EdFileInfoService;
import com.electromagnetic.industry.software.manage.service.EdFileRelationService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/data/ed/repo/relation")
public class RepoEdFileRelationController {

    @Resource
    EdFileRelationService edFileRelationService;

    @Resource
    EdFileInfoService edFileInfoService;

    /**
     * 创建文件关系
     *
     * @param relation
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @UserOperation(value = "创建文件关系", modelName = UserOperationModuleEnum.FILE_RELATION)
    public ElectromagneticResult<?> createRelation(@RequestBody EdFileRelation relation) {
        return ElectromagneticResultUtil.success(edFileRelationService.createRelation(relation));
    }

    /**
     * 更新文件关系
     *
     * @param updateRelation
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @UserOperation(value = "更新文件关系", modelName = UserOperationModuleEnum.FILE_RELATION)
    public ElectromagneticResult<?> updateRelation(@RequestBody UpdateRelationDTO updateRelation) {
        LambdaUpdateWrapper<EdFileRelation> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(EdFileRelation::getId, updateRelation.getRelationId()).set(EdFileRelation::getRelationship, updateRelation.getRelationship());
        boolean isUpdated = edFileRelationService.update(wrapper);
        if (isUpdated) {
            UserThreadLocal.setSuccessInfo("", updateRelation.getRelationId(), "更新了文件关系");
        }
        return ElectromagneticResultUtil.success(isUpdated);
    }

    /**
     * 取消文件关系
     *
     * @param relationId 关系主键id
     * @return
     */
    @RequestMapping(value = "/cancel/{relationId}", method = RequestMethod.GET)
    @UserOperation(value = "取消文件关系", modelName = UserOperationModuleEnum.FILE_RELATION)
    public ElectromagneticResult<?> cancelRelation(@PathVariable("relationId") String relationId) {
        return ElectromagneticResultUtil.success(edFileRelationService.cancelRelation(relationId));
    }

    /**
     * 展示文件关系
     */
    @RequestMapping(value = "listRelations/{id}", method = RequestMethod.GET)
    @UserOperation(value = "查看文件关系", modelName = UserOperationModuleEnum.FILE_RELATION)
    public ElectromagneticResult<?> listRelations(@PathVariable("id") String id) {
        return ElectromagneticResultUtil.success(edFileRelationService.listRelations(id));
    }

    /**
     * 检验文件名是否唯一
     */
    @RequestMapping(value = "/checkFileNameExist", method = RequestMethod.POST)
    @UserOperation(value = "校验文件名唯一性", modelName = UserOperationModuleEnum.FILE_RELATION)
    public ElectromagneticResult<?> checkFileNameExist(@RequestBody CheckNameUniqueRequest checkNameUniqueRequest) {
        return ElectromagneticResultUtil.success(edFileRelationService.checkNameExist(checkNameUniqueRequest));
    }

    /**
     * 本地上传并建立关系
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @UserOperation(value = "上传了文件并创建文件关系", modelName = UserOperationModuleEnum.FILE_RELATION)
    public ElectromagneticResult<?> uploadRelation(@RequestParam("parentId") String parentId,
                                                   @RequestParam("file") MultipartFile file,
                                                   @RequestParam("description") String description,
                                                   @RequestParam("id") String id) {
        return ElectromagneticResultUtil.success(edFileInfoService.uploadFileAndRelation(parentId, id, file, description, DataOwnEnum.REPO_FILE.code));
    }
}
